package cn.shengchao.examstarvideo.config.idempotent;

import cn.hutool.crypto.digest.MD5;
import cn.shengchao.examstarvideo.config.idempotent.generator.IdempotentNameGenerator;
import cn.shengchao.examstarvideo.context.UserContextHolder;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

@Component
public class MyIDempotentNameGenerator implements IdempotentNameGenerator {
    @Override
    public String generate(Object target, Method method, Object... args) {
        return MD5.create().digestHex16(method.getName() + Arrays.stream(args).filter(Objects::nonNull).map(JSON::toJSONString).toList() + UserContextHolder.getUserId());
    }
}
